#include "window4.h"
#include <QApplication>
#include <QDebug>
#include <QScreen>
#include <QNetworkInterface>
#include <QKeyEvent>
Window4::Window4(QWidget *parent) : QMainWindow(parent)
{
    setWindowTitle("系统操作");
    setGeometry(300, 200, 800, 600);
    //屏幕分辨率， 网络ip信息
    showSystemMsg();
}

void Window4::showSystemMsg(){
    //显示屏幕分辨率
    displayLabel = new QLabel(this);
    //QLabel 位置
    displayLabel->setGeometry(QRect(50,50,200,25));

    //实例 QDesktopWidget
   // desktopWidget = QApplication::desktop();
    //实例 QRect 接收屏幕信息 (过时了)
   // QRect screen = desktopWidget->screenGeometry();

    QScreen *  screen = QGuiApplication::primaryScreen();
    QRect rect = screen->geometry();

    //定义字符串
    QString str = "";
    //屏幕宽度
    int sWidth =rect.width();
    //屏幕高度
    int sHeight = rect.height();
    //输出结果
    displayLabel->setText(QString::asprintf("屏幕分辨率为:%d * %d", sWidth, sHeight));
    /**
      需要在.pro文件加入： QT += network
     */
    //获取计算机名称
    QString localHostName = QHostInfo::localHostName();
    qDebug() << "计算机名:" << localHostName;

    //获取 IP 地址
    QHostInfo info = QHostInfo::fromName(localHostName);
    QList<QHostAddress> list = info.addresses();
    //遍历地址,只获取 IPV4 地址
    for(QHostAddress address: list){
        if(address.protocol() == QAbstractSocket::IPv4Protocol){
            qDebug() << "ipV4 地址:" << address.toString();
        }
    }

    //获取ip
    QList<QHostAddress> l0 =QNetworkInterface::allAddresses();
      foreach (QHostAddress address, l0)
      {
         if(address.protocol() ==QAbstractSocket::IPv4Protocol)
             //我们使用IPv4地址
             qDebug() << "ipv4地址：" << address.toString();
      }



    //通过QNetworkInterface类来获取本机的IP地址和网络接口信息
       QList<QNetworkInterface> l = QNetworkInterface::allInterfaces();
       //获取所有网络接口的列表
       foreach(QNetworkInterface interface,l)
       {
           //遍历每一个网络接口
           qDebug() << "Device: "<<interface.name();

           //设备名
           qDebug() << "HardwareAddress:"<<interface.hardwareAddress();

           //硬件地址
           QList<QNetworkAddressEntry> entryList = interface.addressEntries();

           //获取IP地址条目列表，每个条目中包含一个IP地址，一个子网掩码和一个广播地址
           foreach(QNetworkAddressEntry entry,entryList)
           {
               //遍历每一个IP地址条目
               qDebug()<<"IP Address: "<<entry.ip().toString();

               //子网掩码
               qDebug()<<"Netmask: "<<entry.netmask().toString();

               //广播地址
               qDebug()<<"Broadcast: "<<entry.broadcast().toString();

               qDebug()<<"\n";
           }
       }








    //根据网址获取ip地址
    urlLabel = new QLabel(this);
    urlLabel->setGeometry(QRect(50,100,40,25));
    urlLabel->setText("网址:");

    //实例 QLineEdit
    urlEdit = new QLineEdit(this);
    urlEdit->setGeometry(QRect(100,100,150,25));
    urlEdit->setText("www.baidu.com");

    //实例 QPushButton
    sendBtn = new QPushButton(this);
    sendBtn->setGeometry(QRect(260, 100,80,25));
    sendBtn->setText("查询");

    myIpLabel = new QLabel(this);
    myIpLabel->setGeometry(QRect(50,200,40,25));
    myIpLabel->setText("IP地址：");

    connect(sendBtn,SIGNAL(clicked()),this,SLOT(sendUrl()));


}

//键盘事件
void Window4::keyPressEvent(QKeyEvent *event){
    //键值如果等于 Q
    if(event->key()==Qt::Key_Q)
    {
        qDebug() << "按下了Q键盘" << "\n";
    }else{
        qDebug() << event->key() <<"\n";
    }

}

void Window4::sendUrl(){
    QHostInfo::lookupHost(urlEdit->text(),this,SLOT(lookedUp(QHostInfo)));

}

void Window4::lookedUp(QHostInfo hostInfo){
    myIpLabel->setText("IP地址 结果："+hostInfo.addresses().first().toString());
}




